<?php
session_start();

$servername = "localhost";
$username = "root";
$password = "123456";
$dbname = "userdb";

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = $_POST['username'];
    $password = $_POST['password'];

    // 查询用户
    $sql = "SELECT * FROM users WHERE username = '$username'";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        // 验证密码
        if (password_verify($password, $row['password_hash'])) {
            $_SESSION['username'] = $username;
            header('Location: welcome.php'); // 更改为你的成功登录后跳转页面
            exit();
        } else {
            echo "密码错误";
        }
    } else {
        echo "用户不存在";
    }
}

$conn->close();
?>